﻿using System;
using System.Globalization;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BookiFramework.Portable.Test
{
    [TestClass]
    public class FilesTest
    {
        [TestMethod]
        public void Test_read_text_works()
        {
            //Arrange
            const string text = "Booki framework is cool";
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);
            writer.WriteLine(text);
            writer.Flush();            
            //Act

            var result = new Files.Files().ReadText(stream);

            //Assert
            Assert.AreEqual(result.Trim(),text.Trim(),ignoreCase:true);
        }

        [TestMethod]
        public void Test_write_text_works_without_append_without_close()
        {
            //Arrange
            const string text = "Booki framework is cool";
            var stream = new MemoryStream();

            var file = new Files.Files();
            //Act
            
            file.WriteText(stream,text,append:false,closeStream:false);

            var result = file.ReadText(stream);
            //Assert
            Assert.AreEqual(result.Trim(), text.Trim(), ignoreCase: true);
        }

        [TestMethod]
        public void Test_write_text_works_with_append_without_close()
        {
            //Arrange
            const string text = "Booki framework is cool";
            const string otherLine = "I like TDD";

            var resultConcat = String.Format("{0}\n{1}", text, otherLine);

            var stream = new MemoryStream();
            var file = new Files.Files();
            //Act

            file.WriteText(stream, text, append: false, closeStream: false);
            file.WriteText(stream, otherLine, append: true, closeStream: false);

            var result = file.ReadText(stream);
            //Assert
            Assert.AreNotEqual(result.Trim(), text.Trim(), ignoreCase: true);
            Assert.AreEqual(result.Trim().Length, resultConcat.Trim().Length);
        }
        
        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void Test_write_text_works_with_close()
        {
            //Arrange
            const string text = "Booki framework is cool";
            var stream = new MemoryStream();

            var file = new Files.Files();
            //Act

            file.WriteText(stream, text, append: false, closeStream: true);
            var result = file.ReadText(stream);
            
            //Assert
            Assert.Inconclusive();
        }
    }
}
